package com.sugar.queryinfo.module.business.service.userauth;

import cn.hutool.core.collection.CollUtil;
import com.sugar.queryinfo.module.system.api.dept.DeptApi;
import com.sugar.queryinfo.module.system.api.user.AdminUserApi;
import com.sugar.queryinfo.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.sugar.queryinfo.module.business.controller.admin.userauth.vo.*;
import com.sugar.queryinfo.module.business.dal.dataobject.userauth.UserAuthDO;
import com.sugar.queryinfo.framework.common.pojo.PageResult;
import com.sugar.queryinfo.framework.common.pojo.PageParam;
import com.sugar.queryinfo.framework.common.util.object.BeanUtils;

import com.sugar.queryinfo.module.business.dal.mysql.userauth.UserAuthMapper;

import static com.sugar.queryinfo.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.queryinfo.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.queryinfo.framework.common.util.collection.CollectionUtils.diffList;
import static com.sugar.queryinfo.framework.common.util.servlet.ServletUtils.getClientIP;
import static com.sugar.queryinfo.module.business.enums.ErrorCodeConstants.*;
import static com.sugar.queryinfo.module.system.enums.ErrorCodeConstants.DEPT_NOT_FOUND;
import static com.sugar.queryinfo.module.system.enums.ErrorCodeConstants.USER_NOT_EXISTS;

/**
 * 用户授权维护 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class UserAuthServiceImpl implements UserAuthService {

    @Resource
    private UserAuthMapper userAuthMapper;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createUserAuth(UserAuthSaveReqVO createReqVO) {
        AdminUserRespDTO user = adminUserApi.getUser(createReqVO.getUserId());
        if (user == null)
            throw exception(USER_NOT_EXISTS);
        Long deptId = user.getDeptId();
        if (deptId == null)
            throw exception(DEPT_NOT_FOUND);
        // 插入
        UserAuthDO userAuth = BeanUtils.toBean(createReqVO, UserAuthDO.class);
        // 获取部门信息
        userAuth.setStatus(1).setDeptId(deptId).setLocation(getClientIP());
        userAuthMapper.insert(userAuth);

        // 返回
        return userAuth.getId();
    }

    @Override
    public void updateUserAuth(UserAuthUpdateReqVO updateReqVO) {
        // 校验存在
        validateUserAuthExists(updateReqVO.getId());
        // 更新
        UserAuthDO updateObj = BeanUtils.toBean(updateReqVO, UserAuthDO.class);
        userAuthMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserAuth(Long id) {
        // 校验存在
        validateUserAuthExists(id);
        // 删除
        userAuthMapper.deleteById(id);
    }

    @Override
        public void deleteUserAuthListByIds(List<Long> ids) {
        // 删除
        userAuthMapper.deleteByIds(ids);
        }


    private void validateUserAuthExists(Long id) {
        if (userAuthMapper.selectById(id) == null) {
            throw exception(USER_AUTH_NOT_EXISTS);
        }
    }

    @Override
    public UserAuthDO getUserAuth(Long id) {
        return userAuthMapper.selectById(id);
    }

    @Override
    public PageResult<UserAuthDO> getUserAuthPage(UserAuthPageReqVO pageReqVO, Long userId) {
        return userAuthMapper.selectPage(pageReqVO, userId);
    }

}